package TestPaperSystem.config.activeMQ;

import TestPaperSystem.enmus.PaperStatus;
import TestPaperSystem.entity.Paper;
import TestPaperSystem.service.MsgService;
import TestPaperSystem.service.PaperService;
import TestPaperSystem.utils.DateGetter;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Listener {

    @Autowired
    PaperService paperServiceImpl;

    @Autowired
    MsgService msgServiceImpl;

    @JmsListener(destination = "delay_Queue")
    public void changePaperStatus(@NotNull String paperId) throws Exception {
        if(paperId != null){
            Paper paper = paperServiceImpl.getById(paperId);
            if(paper != null && paper.getState() == PaperStatus.ING){
                paper.setState(PaperStatus.BREAK);
                paper.setObjScore(0);
                paper.setSubjScore(0);
                paper.setUserScore(0);
                paper.setUserTime(paper.getTotalTime());
                paperServiceImpl.updateMsg(paper);
                paperServiceImpl.updateById(paper);
            }
        }
    }

    @JmsListener(destination = "sendRegister")
    public void sendRegisterEmail(String email){
        msgServiceImpl.sendMsg(email);
    }
}
